package com.sctvcloud.zhaohua_sd.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.MessageInfo;

import java.util.List;

public class MessageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MessageInfo> messageList;
    private OnItemClickListener listener;
    private Context ctx;

    static class MessageViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout messageLayout;
        ImageView msgTip;
        CustomFontTextView messageContent;
        CustomFontTextView messageTime;
        View divider;

        public MessageViewHolder(View view) {
            super(view);
            messageLayout = (RelativeLayout)view.findViewById(R.id.message_layout);
            msgTip = (ImageView) view.findViewById(R.id.read_tip);
            messageContent = (CustomFontTextView) view.findViewById(R.id.message_content);
            messageTime = (CustomFontTextView) view.findViewById(R.id.message_time);
            divider = (View)view.findViewById(R.id.divider_line);
        }
    }


    public MessageListAdapter(Context context, List<MessageInfo> list) {
        messageList = list;
        ctx = context;
    }

    public OnItemClickListener getListener() {
        return listener;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setData(List<MessageInfo> list) {
        if (null != list) {
            messageList = list;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list_item, parent, false);
        holder = new MessageViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final MessageInfo info = messageList.get(position);
        final MessageViewHolder viewHolder = (MessageViewHolder) holder;
        if(info.getStatus()==1){
            //已读
            viewHolder.msgTip.setVisibility(View.INVISIBLE);
        }else {
            //未读
            viewHolder.msgTip.setVisibility(View.VISIBLE);
        }
        viewHolder.messageContent.setText(info.getContent());
        String time = info.getDate();
        if(!TextUtils.isEmpty(time) ){
            if(time.length()>10){
                time = time.replace(" ","\n");
            }
            viewHolder.messageTime.setText(time);
        }


        viewHolder.messageLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onItemClick(info);
                }
            }
        });

        if(position == getItemCount()-1){
            viewHolder.divider.setVisibility(View.GONE);
        }else {
            viewHolder.divider.setVisibility(View.VISIBLE);
        }

    }

    //返回子项个数
    @Override
    public int getItemCount() {
        if (null != messageList && messageList.size() > 0) {
            return messageList.size();
        }
        return 0;
    }

    public interface OnItemClickListener {
        void onItemClick(MessageInfo msgInfo);
    }

}
